/**
 * 
 */
package edu.gtcfla.km.platform.service;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.vsg.biz.account.Menu;
import org.vsg.biz.account.Url;
import org.vsg.biz.account.repositories.MenuRepo;
import org.vsg.biz.account.repositories.UrlRepo;
import org.vsg.module.resources.ResourceItemView;
import org.vsg.module.resources.UrlSupport;
import org.vsg.module.resources.web.WebMenuAction;
import org.vsg.module.resources.web.WebMenuItem;
import org.vsg.module.resources.web.discover.WebMenuDiscoveryService;

/**
 * @author Bill Vison
 *
 */
public class CrmMenuManagerServiceImpl implements InitializingBean,
		WebMenuDiscoveryService {
	
	@Resource
	private MenuRepo menuRepo;
	
	@Resource
	private UrlRepo urlRepo;
	
	private final static Logger logger = LoggerFactory.getLogger(CrmMenuManagerServiceImpl.class);
	
	
	private Map<String , WebMenuAction> menuActionHashmap = new LinkedHashMap<String , WebMenuAction>();	

	/* (non-Javadoc)
	 * @see org.vsg.module.resources.web.discover.WebMenuDiscoveryService#findAllMenuItemsByLevel(int)
	 */
	public Set<WebMenuItem> findAllMenuItemsByLevel(int level) {
		// TODO Auto-generated method stub
		
		Set<Menu> resSet = (Set<Menu>) menuRepo.findAllByLevel(level);
		
		Set<WebMenuItem> itemSet = new LinkedHashSet<WebMenuItem>();
		
		// --- get the resource set ---
		for (Menu res : resSet) {
			WebMenuItem mainItem = new WebMenuItem();
			mainItem.setResId( res.getId() );
			mainItem.setMenuCode(res.getCode() );
			mainItem.setName( res.getName() );
			mainItem.setEnabled(  res.getDisabled() == 0 );
			
			
			// ---- add action for menu item ---
			String keyAction = res.getKeyAction();
			if ( keyAction.startsWith("url://")) {
				String key = keyAction.substring( "url://".length() );
				Url sysUrl = urlRepo.getByCode( key );

				WebMenuAction menuAction = new WebMenuAction();
				menuAction.setResourceItem(mainItem);
				menuAction.setUrl(  sysUrl.getLink() );
				
				if (sysUrl.getType().equals("internal")) {
					menuAction.setUrlSupportedType( UrlSupport.TYPE_INTERNAL_APPS);
				}
				menuActionHashmap.put( mainItem.getResId() , menuAction);	

			}


			itemSet.add( mainItem );

		}
		
		
		return itemSet;
	}

	/* (non-Javadoc)
	 * @see org.vsg.module.resources.web.discover.WebMenuDiscoveryService#getMenuActionByMenu(org.vsg.module.resources.ResourceItemView)
	 */
	public WebMenuAction getMenuActionByMenu(ResourceItemView item) {
		// TODO Auto-generated method stub
		WebMenuAction menuAction = this.menuActionHashmap.get(  item.getResId());
		return menuAction;
	}

	/* (non-Javadoc)
	 * @see org.vsg.module.resources.web.discover.WebMenuDiscoveryService#findAllMainMenuItems()
	 */
	public Set<WebMenuItem> findAllMainMenuItems() {
		// TODO Auto-generated method stub
		return findAllMenuItemsByLevel(0);
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}

}
